﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using GPS.Models;


namespace GPS.Controllers
{
    public class TestController : Controller
    {
        //
        // GET: /Test/
        public ActionResult Test()
        {
            return View("View1");
        }

        [HandleError]
        public ActionResult Login()
        {
            string name = Request.Form["UserName"].ToString();
            string ActionMethod = Request.Form["BtnSubmit"].ToString();


            int temp_i = int.Parse(Request.Form["BtnSubmit"].ToString());


            //if (string.IsNullOrEmpty(name))
            //    return View("View2");
            if (ActionMethod == "Login")
            {
                //是登录操作 
               FormsAuthentication.SetAuthCookie(name, false);//给浏览器返回一个cookies，并且保存一个name
               //return Content("已经登录");
            }
            else
            {
                //是注销操作
                FormsAuthentication.SignOut();
                //return Content("没有登录");
            }
            //问题在这里 如果用RedirectToAction返回，页面里面就正常显示登入登出状态
            //return RedirectToAction("Test");

            //如果直接返回View，页面还是显示没登录，需要点击浏览器刷新，页面才能正常显示登入登出状态  
            return View("View1");

            //return Content("已经登录");


        }
        [ChildActionOnly]
        public ActionResult LoginState()
        {
            if(this.User.Identity.IsAuthenticated)
            {
                return Content("<div style='color:red'>输出字符串</div>");
            }
            return Content("<h1>输出字符串<h1>");

            
        }

        public ActionResult TestSubmit(UserDetails details)
        {

            return View("View2", details);
        }

        [HttpPost]
        public ActionResult Upload(FileUploadViewModel file)
        {
           

            //model.fileupload.SaveAs("E:\\Project\\GPS\\GPS\\GPS\\Content\\up.txt");
            return RedirectToAction("Test", "Test");
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult test2(UserDetails details)
        {
            ViewBag.Notice = Request.Form["Notice"].ToString();
            return View("View2", details);  
        }

        public ActionResult View2(UserDetails details)
        {
            ViewBag.Notice = "Original View2";
            return View("View2", details);  
        }

        public ActionResult View3()
        {
            List<Dictionary<string, object>> list = new List<Dictionary<string, object>>();
            Dictionary<string, object> map = new Dictionary<string, object>();
            map.Add("aaa", "a1");
            map.Add("bbb", "b1");
            map.Add("ccc", "c1");
            list.Add(map);
            map.Clear();
            map.Add("aaa", "a2");
            map.Add("bbb", "b2");
            map.Add("ccc", "c2");
            list.Add(map);
            map.Clear();
            map.Add("aaa", "a3");
            map.Add("bbb", "b3");
            map.Add("ccc", "c3");
            list.Add(map);



            ViewBag.Notice = "Original View2";
            return Json(list, JsonRequestBehavior.AllowGet);
        }
    }
}
